<script setup>

</script>

<template>
  <el-card>
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
            v-model="queryParams.menuName"
            placeholder="请输入菜单名称"
            clearable
            size="small"
            style="width:240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            size="small"
            style="width:200px"
        >
          <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->
    <!-- 数据表格开始 -->
    <el-table
        border
        :data="menuTableList"
        row-key="menuId"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="菜单名称" align="left" prop="menuName"/>
      <el-table-column label="菜单类型" align="center" prop="menuType">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.menuType==='M'?'primary':scope.row.menuType==='C'?'danger':'success'"
          >
            {{ scope.row.menuType === 'M'
              ? '目录' : scope.row.menuType === 'C'
                  ? '菜单' :scope.row.menuType === 'F'
                      ? '按钮' : '未知'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="组件路径" align="center" prop="path"/>
      <el-table-column label="权限标识" align="center" prop="percode"/>
      <el-table-column label="状态" align="center" prop="statusName"/>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button  type="text"
                      icon="el-icon-plus"
                      size="mini" @click="handleAdd(scope.row)"
                      v-if="scope.row.menuType === 'M'||scope.row.menuType === 'C'">
            添加
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 添加和修改的弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="600px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="parentName">
              <el-input
                  v-if="form.selectStatus === 1"
                  v-model="form.fatherMenuName"
                  :disabled="form.path!=null"/>

              <el-input v-else value="主目录" disabled></el-input>
              <el-input v-else v-model="form.fatherMenuId" style="display: none" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="menuType">
              <el-radio-group v-model="form.menuType" >
                <el-radio label="M">目录</el-radio>
                <el-radio label="C">菜单</el-radio>
                <el-radio label="F">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="form.menuName" placeholder="请输入菜单名称" clearable size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路由地址" prop="path">
              <el-input v-model="form.path" placeholder="请按照格式输入" clearable size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.menuType" label="权限标识" prop="percode">
              <el-input v-model="form.percode" placeholder="请输入权限标识" clearable size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    :value="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加和修改的弹出层结束 -->
  </el-card>

</template>


<script>
import {handleTree} from "@/utils/his-utils";

export default {
  data() {
    return {
      queryParams: {
        menuName: undefined,
        status: undefined
      },
      statusOptions: [],
      menuTableList: [],
      //添加或修改的表单数据
      form:{
        path: undefined,
      },
      //表单验证规则
      rules:{},
      //添加或修改表单的弹出
      open: false,
      //添加或修改表单的标题
      title: "",
      menuOptions:[]
    }
  },
  created() {
    this.formdata()
    this.loadStatus()
  },
  methods: {
    //表单搜索
    handleQuery() {
      this.formdata()
    },
    //重置搜索表单
    resetQuery() {
      this.queryParams = {}
      this.formdata()
    },
    //获取表单数据
    formdata() {
      this.$http.post("/menu/queryByPage", this.queryParams).then(resp => {
        if (resp.data.code === 200) {
          this.menuTableList = handleTree(resp.data.data, "menuId")
        }
      })
    },
    getButtons() {
      //查询按钮权限
      this.$http.get("/menu/getButtons").then(res=>{
        localStorage.setItem(
            "userButtonPermissions",
            res.data.data
        );
      })
    },
    // 自定义键名
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      }
    },
    //查询状态字典
    loadStatus() {
      this.$http.get("/menu/getStatus").then(resp => {
        this.statusOptions = resp.data.data
      })
    },
    //添加
    handleAdd(row) {
      console.log(row)
      this.formReSet()
      this.form.menuType = "M"
      this.form.parentId = 0
      if (row != null) {
        this.form.parentId = row.menuId
        if (row.menuType === 'M') {
          this.form.menuType = 'C'
        } else if (row.menuType === 'C') {
          this.form.menuType = 'F'
        }
      }
      if (row.menuId != null) {
        this.form.selectStatus = 1
        this.form.path = row.path
        this.form.fatherMenuName = row.menuName
        this.form.fatherMenuId = row.menuId
      }
      if (row.parentId!==0){
        //说明是儿子菜单添加 菜单或者按钮，让菜单类型可以进行选择

      }
      this.open = true

      this.title = '---添加菜单---'
    },
    //添加或修改的提交
    handleSubmit(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$http.post("/menu/insertOrUpa", this.form).then(resp => {
            if (resp.data.code === 200) {
              this.$message.success('操作成功')
              this.form={}
              this.open = false
              this.formdata()
              this.getButtons()
            }else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },
    //添加或修改的取消
    cancel() {
      this.open = false
      this.form = {}
      this.formdata()
    },
    //表单重置
    formReSet(){
      this.form = {
        menuType: "M",
        status: "0",
        selectStatus: 0,
        fatherMenuId: 0,
        fatherMenuName: "",
        path: undefined,
        menuName: undefined,
        percode: undefined,
        remark: undefined
      }
      this.formdata()
    },
    //修改
    handleUpdate(row) {
      this.open = true
      this.title = '---修改菜单---'
     /* this.$http.get("/system-manage/system/menu/getMenuById/" + row.menuId).then(resp => {
        if (resp.data.code === 200) {
          this.form = resp.data.data
        }
      })*/
      this.form = row
      this.menuTableList.filter(item => {
        if (item.menuId === row.parentId) {
          this.form.fatherMenuId = item.menuId
          this.form.selectStatus = 1
          this.form.fatherMenuName = item.menuName
        }
      })
    },
    //删除
    handleDelete(row) {
      this.$confirm("确认删除选中的项吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
      this.$http.delete("/menu/deleteById/" + row.menuId).then(resp => {
        if (resp.data.code === 200) {
          this.$message.success("删除成功");
          this.formdata()
          this.getButtons()
        }
      }).catch(error =>{
            this.$message.warning("删除失败");
      })
      }).catch(error => {
        this.$message.warning("取消删除");
      })
    },
    //格式化状态
    statusFormatter(row) {
      let dictLabel = "未知"
      this.statusOptions.filter(item => {
        if (item.dictValue === row.status) {
          dictLabel = item.dictLabel
        }
      })
      return dictLabel
    }
  }
}
</script>
<style scoped>

</style>